/*
    \file    adxl355.c
    \brief   collect code for adxl355
*/

#include "main.h"

#define  ARRAYSIZE         10
uint32_t send_n = 0, receive_n = 0;
uint8_t spi2_receive_array[ARRAYSIZE];

static void spi2_rcu_config(void);
static void spi2_gpio_config(void);
static void spi2_config(void);

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int spi_slave_init(void)
{
    /* NVIC configure */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
    nvic_irq_enable(SPI2_IRQn,2,0);

    /* peripheral clock enable */
    spi2_rcu_config();
    /* GPIO configure */
    spi2_gpio_config();
    /* SPI configure */
    spi2_config();
    /* SPI interrupt enable */
    spi_i2s_interrupt_enable(SPI2, SPI_I2S_INT_RBNE);

    /* SPI enable */
    spi_enable(SPI2);

    /* wait transmit complete */
    while(receive_n < ARRAYSIZE);
    while(1);
   
}

/*!
    \brief      configure different peripheral clocks
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void spi2_rcu_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_AF);
    rcu_periph_clock_enable(RCU_SPI2);
}

/*!
    \brief      configure the GPIO peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void spi2_gpio_config(void)
{   
    /* SPI2 GPIO config:SCK/PB3, MOSI/PB5 MISO/PB4*/
		gpio_pin_remap_config(GPIO_SPI2_REMAP, DISABLE);
		gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5);
		gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
}

/*!
    \brief      configure the SPI peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void spi2_config(void)
{
    spi_parameter_struct spi_init_struct;
    /* deinitilize SPI and the parameters */
    spi_i2s_deinit(SPI2);
    spi_struct_para_init(&spi_init_struct);

    /* configure SPI2 parameter */
    spi_init_struct.trans_mode  = SPI_TRANSMODE_BDRECEIVE;
    spi_init_struct.device_mode = SPI_SLAVE;
    spi_init_struct.nss         = SPI_NSS_SOFT;
    spi_init(SPI2, &spi_init_struct);
	
    spi_nss_internal_low(SPI2);   
}



